<script>
import {addOrUpdateUser, removeUsers, userPage} from "@/api/user";

export default {
  name: "UserView",
  data() {
    return {
      formLabelWidth: '80px',
      title: '新增',
      dialogFormVisible: false,   // 隐藏弹窗
      form: {
        id: '',
        username: '',
        nickname: '',
        age: '',
        email: ''
      },
      tableData: [],
      currentPage: 1,
      size: 5,
      total: 20,
      selectionId: []
    }
  },
  // 单击事件的回调函数
  methods: {
    // 每页展示条数改变时触发
    handleSizeChange(val) {
      this.size = val
      this.initUserPage()
    },
    // 改变页码时自动触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.initUserPage()
    },
    // 编辑数据的回调函数
    handleEdit(index, row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      // 数据回显
      this.form = row
    },
    // 删除数据的回调函数
    handleDelete(index, row) {

      // 用户的确认框
      this.$confirm('此操作将永久删除行数据, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeUsers([row.id]).then(response=>{
          if (response.code == 200) {
            this.$message.success(response.message)
            this.initUserPage()
          }else{
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 复选框回调函数  val 所有被选中的行
    handleSelectionChange(val) {
      this.selectionId = val.map(v => v.id)
      console.log(JSON.stringify(this.selectionId))
    },
    // 提交表单数据
    onSubmit() {
      console.log(JSON.stringify(this.form))
      // http请求     添加表单数据   后台  insert
      addOrUpdateUser(this.form).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          // 关闭弹窗
          this.dialogFormVisible = false
          // TODO  重载表格数据
          this.initUserPage()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    //清空数据
    clearInfo() {
      this.title = '新增'
      this.form = {
        id: '',
        username: '',
        nickname: '',
        age: '',
        email: ''
      }
    },
    initUserPage() {
      userPage(this.currentPage, this.size).then(response => {
        this.total = response.count
        this.tableData = response.data
      })
    },
    deleteUser() {

      if (this.selectionId.length==0){
        this.$message.warning('请至少选择一条数据...')
        return false
      }

      removeUsers(this.selectionId).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.initUserPage()
        }else {
          this.$message.error(response.message)
        }
      })
    }
  },
  created() {
    this.initUserPage()
  }
}
</script>

<template>
  <!--  所有的标签需要放置在一个根标签中-->
  <div id="container">
    <div id="btn-header">
      <el-button type="primary" plain size="small" @click="dialogFormVisible=true">新增</el-button>
      <el-button type="success" plain size="small" @click="deleteUser">批量删除</el-button>
    </div>
    <el-table
        stripe
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 100%">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="username"
          label="姓名">
      </el-table-column>
      <el-table-column
          prop="nickname"
          label="昵称">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱">
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div id="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div id="dialog">
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
        <el-form :model="form">
          <!--          隐藏域-->
          <el-input type="hidden" v-model="form.id" autocomplete="off"></el-input>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<!--编写css样式-->
<style scoped>
#btn-header {
  /*调整按钮的位置*/
  text-align: left;
  margin-bottom: 10px;
  margin-top: 15px;
}

#container {
  padding: 20px;
}

#pagination {
  margin-top: 15px;
}

#dialog {
  text-align: left;
}
</style>